#include <stdio.h>
#include <stdlib.h>
typedef struct NODE
{
int data;
struct NODE *next;
};
NODE *head = NULL;
void addAtStart(int data)
{
NODE *newNode = (NODE *)malloc(sizeof(NODE));
newNode->data = data;
newNode->next = head;
head = newNode;
}
void addAtEnd(int data)
{
NODE *newNode = (NODE *)malloc(sizeof(NODE));
newNode->data = data;
newNode->next = NULL;
if (head == NULL)
{
head = newNode;
return;
}
NODE *curr = head;
while (curr->next != NULL)
{
curr = curr->next;
}
curr->next = newNode;
}
void addBefore(int key, int data)
{
NODE *newNode = (NODE *)malloc(sizeof(NODE));
newNode->data = data;
newNode->next = NULL;
if (head == NULL)
{
printf("List is empty\n");
return;
}
if (head->data == key)
{
newNode->next = head;
head = newNode;
return;
}
NODE *curr = head;
while (curr->next != NULL && curr->next->data != key)
{
curr = curr->next;
}
if (curr->next == NULL)
{
printf("Key not found\n");
return;
}
newNode->next = curr->next;
curr->next = newNode;
}
void addAfter(int key, int data)
{
NODE *newNode = (NODE *)malloc(sizeof(NODE));
newNode->data = data;
newNode->next = NULL;
if (head == NULL)
{
printf("List is empty\n");
return;
}
NODE *curr = head;
while (curr != NULL && curr->data != key)
{
curr = curr->next;
}
if (curr == NULL)
{
printf("Key not found\n");
return;
}
newNode->next = curr->next;
curr->next = newNode;
}
void deleteStart()
{
if (head == NULL)
{
printf("List is empty\n");
return;
}
NODE *temp = head;
head = head->next;
free(temp);
}
void deleteEnd()
{
if (head == NULL)
{
printf("List is empty\n");
return;
}
if (head->next == NULL)
{
free(head);
head = NULL;
return;
}
NODE *curr = head;
while (curr->next->next != NULL)
{
curr = curr->next;
}
free(curr->next);
curr->next = NULL;
}
void deleteKey(int key)
{
if (head == NULL)
{
printf("List is empty\n");
return;
}
if (head->data == key)
{
NODE *temp = head;
head = head->next;
free(temp);
return;
}
NODE *curr = head;
while (curr->next != NULL && curr->next->data != key)
{
curr = curr->next;
}
if (curr->next == NULL)
{
printf("Key not found\n");
return;
}
NODE *temp = curr->next;
curr->next = curr->next->next;
free(temp);
}
void printList()
{
NODE *curr = head;
while (curr != NULL)
{
printf("%d ", curr->data);
curr = curr->next;
}
printf("\n");
}
int main()
{
int choice, data, key;
while (1)
{
printf("\n1. Add at start\n2. Add at end\n3. Add before\n4. Add after\n");
printf("5. Delete start\n6. Delete end\n7. Delete key\n8. Print list\n9. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter data to add at start: ");
scanf("%d", &data);
addAtStart(data);
break;
case 2:
printf("Enter data to add at end: ");
scanf("%d", &data);
addAtEnd(data);
break;
case 3:
printf("Enter key before which to add data: ");
scanf("%d", &key);
printf("Enter data to add before %d: ", key);
scanf("%d", &data);
addBefore(key, data);
break;
case 4:
printf("Enter key after which to add data: ");
scanf("%d", &key);
printf("Enter data to add after %d: ", key);
scanf("%d", &data);
addAfter(key, data);
break;
case 5:
deleteStart();
break;
case 6:
deleteEnd();
break;
case 7:
printf("Enter key to delete: ");
scanf("%d", &key);
deleteKey(key);
break;
case 8:
printf("List: ");
printList();
break;
case 9:
exit(0);
default:
printf("Invalid choice\n");
}
}
return 0;
}